package me.dr0n3.skyRanks;

import me.dr0n3.skyRanks.Utils.IOHelper;
import me.dr0n3.skyRanks.Utils.PermissionsHelper;
import me.dr0n3.skyRanks.Utils.TimeHelper;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;

/**
 * @author dr0n3
 * @date 17.02.2012
 */
public class skyPlayerListener implements Listener
{
    // <editor-fold defaultstate="collapsed" desc="Variablen">
    skyRanks plugin;
    // </editor-fold>
    
    skyPlayerListener(skyRanks instance)
    {
        plugin = instance;
    }
    
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e)
    {
        TimeHelper.hmLoginTime.put(e.getPlayer(), System.currentTimeMillis());
        PermissionsHelper.checkRanks(e.getPlayer());
    }
    
    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent e)
    {
        try
        {
            long playtime = TimeHelper.hmLoginTime.get(e.getPlayer());
            TimeHelper.hmLoginTime.remove(e.getPlayer());
            IOHelper.savePlaytime(e.getPlayer(), playtime);
            
            if(plugin.lBlocked.contains(e.getPlayer()))
                plugin.lBlocked.remove(e.getPlayer());
        }
        catch(Exception ex)
        {
            
        }
        
    }
    
    @EventHandler
    public void onPlayerKick(PlayerKickEvent e)
    {
        try
        {
            long playtime = TimeHelper.hmLoginTime.get(e.getPlayer());
            TimeHelper.hmLoginTime.remove(e.getPlayer());
            IOHelper.savePlaytime(e.getPlayer(), playtime);
            
            if(plugin.lBlocked.contains(e.getPlayer()))
                plugin.lBlocked.remove(e.getPlayer());
        }
        catch(Exception ex)
        {
            
        }
    }
}
